﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Reflection;
using System.ServiceModel.Activation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Client.Services;
using System.ServiceModel.Web;
using System.Net;

namespace Sukul.SharePoint.Taxomatic.Service
{
    //[BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class TaxomaticService : ServiceBase, ITaxomaticService
    {
        private DTO.WebServiceRequest GetRequestObject(string request)
        {
            DTO.WebServiceRequest requestObj = null;
            if (!string.IsNullOrEmpty(request))
            {
                requestObj = Newtonsoft.Json.JsonConvert.DeserializeObject<DTO.WebServiceRequest>(request);
            }
            return requestObj;
        }
        /// <summary>
        /// Creates a graph of all terms stores, not just the default.
        /// </summary>
        /// <returns>A JSON object of all terms.</returns>
        public string GetTerms(string request)
        {
            base.AddResponseHeader();

            DTO.WebServiceRequest requestObj = null;
            var response = new DTO.WebServiceResponse();
            string sResponse = string.Empty;

            try
            {
                requestObj = GetRequestObject(request);
                response.Payload = TaxonomyHelper.Instance.GetTermTree();
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                base.HandleResponseError(response, ex);
            }
            sResponse = Newtonsoft.Json.JsonConvert.SerializeObject(response);

            return sResponse;
        }

        /// <summary>
        /// Returns information about a single term
        /// </summary>
        /// <param name="termId"></param>
        /// <returns></returns>
        public string GetSingleTerm(string request)
        {
            base.AddResponseHeader();

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            DTO.WebServiceRequest requestObj = null;
            string termId = string.Empty;
            var response = new DTO.WebServiceResponse();
            string sResponse = string.Empty;

            try
            {
                requestObj = GetRequestObject(request);
                termId = requestObj.Args;
                response.IsSuccess = true;
                response.Payload = TaxonomyHelper.Instance.GetSingleTerm(termId);
            }
            catch (Exception ex)
            {
                base.HandleResponseError(response, ex);
            }
            sResponse = Newtonsoft.Json.JsonConvert.SerializeObject(response);
            return sResponse;
        }

        public string GetVersion(string request)
        {
            base.AddResponseHeader();

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            DTO.WebServiceRequest requestObj = null;
            var response = new DTO.WebServiceResponse();
            string sResponse = string.Empty;

            try
            {
                requestObj = GetRequestObject(request);
                var assembly = Assembly.GetExecutingAssembly();
                sResponse = string.Format("<b>Title</b>: {1}{0}<b>Version</b>: {2}{0}<b>Description</b>: {3}",
                    "<br/>",
                    TaxonomyHelper.Instance.GetAssemblyAttribute<AssemblyTitleAttribute>(assembly).Title,
                    TaxonomyHelper.Instance.GetAssemblyAttribute<AssemblyFileVersionAttribute>(assembly).Version,
                    TaxonomyHelper.Instance.GetAssemblyAttribute<AssemblyDescriptionAttribute>(assembly).Description);
                response.Payload = sResponse;
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                base.HandleResponseError(response, ex);
            }
            sResponse = Newtonsoft.Json.JsonConvert.SerializeObject(response);
            return sResponse;
        }

        public string Test()
        {
            return "Service says hello";
        }

    }
}
